package com.jinghangzz.shop.common.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.jinghangzz.shop.common.util.DateUtil;
import com.jinghangzz.shop.common.util.HttpUtil;
import com.jinghangzz.shop.common.util.PageInfoUtil;

/**
 * 所有Controller重复的代码都放到这来
 * @author TeaBig
 */
public class BaseController
{
	@Resource
	protected DateUtil dateUtil ; 
	@Resource
	protected HttpUtil httpUtil ; 
	
	/* 提示信息 */
	protected String info = "操作失败"; 
	
	/**
	 * 生成一个分页的工具类
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfoUtil(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/*-------写死的------*/
		try
		{
			/* 接收参数 */
			String currentPage = request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			/* 设置分页 */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}
	
	/**
	 * 专门处理搜索条件
	 * @param request
	 * @param model
	 * @param condMap
	 */
	protected void proccedSearchCond(HttpServletRequest request, Map<String, Object> condMap)
	{
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		/* 把参数再放到request中 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		/* 把开始时间和结束时间由原来的字符串变成Date类型
		 * 接收时间的时候使用字符串,
		 * 将字符串转换成Date类型使用我们自己写的工作类
		 *  */
		Date stDate = null ; 
		Date edDate = null ; 
		if(st != null && !"".equalsIgnoreCase(st) && ed != null && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.strToDateTime(st);
			edDate = this.dateUtil.strToDateTime(ed);
		}
		/* ----------写死的-------- */
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
	}
}
